<?php 
/**
 * Configuration
 * @author Micou13630
 *
 */
class Config extends Initializer {

	/**
	 * Constructeur prive
	 */
	private function Config() {
		parent::Initializer();
	}
	
	/**
	 * Chargement de la configuration
	 * @param unknown_type $mode
	 */
	public function load($file) {
		$this->initWithXml($file);
	}
	
	/**
	 * Renvoie l'instance du singleton
	 */
	public static function getParams() {
		$file = "glyphe.xml";

		if (!isset($_SESSION['#CFG#']->dateConf) || (file_exists($file) && filemtime($file) > $_SESSION['#CFG#']->dateConf)) {
			$c = __CLASS__;			
			$objCfg = new $c;
			$objCfg->load($file);
			$_SESSION['#CFG#'] = $objCfg;
		}
		return $_SESSION['#CFG#'];
	}
}

/**
 * Configuration
 * @author Micou13630
 *
 */
class Param extends Initializer {
	/**
	 * Constructeur prive
	 */
	private function Config() {
		parent::Initializer();
	}
	
	/**
	 * Chargement de la configuration
	 * @param unknown_type $mode
	 */
	public function load($file) {
		$this->initWithXml($file);
	}
	
	/**
	 * Renvoie l'instance du singleton
	 */
	public static function getParams() {
		$file = "param.xml";

		if (!isset($_SESSION['#PARAM#']->dateConf) || (file_exists($file) && filemtime($file) > $_SESSION['#PARAM#']->dateConf)) {
			$c = __CLASS__;			
			$objCfg = new $c;
			$objCfg->load($file);
			$_SESSION['#PARAM#'] = $objCfg;
		}
		return $_SESSION['#PARAM#'];
	}
}

/**
 * Enter description here ...
 * @author Michael
 *
 */
class Initializer {
	protected $tag;
	protected $dateConf;
	
	/**
	 * Enter description here ...
	 */
	public function Initializer() {
		$this->tag = null;
	}
	
	/**
	 * Enter description here ...
	 * @param unknown_type $file
	 */
	protected function initWithXml($file) {
		$parser = xml_parser_create();
	    xml_set_object($parser, $this);
	    xml_set_element_handler($parser, "tag_open", "tag_close");
        xml_set_character_data_handler($parser, "cdata");
	
        if (file_exists($file)) {
        	$this->dateConf = filemtime($file);
        	
        	$fp = fopen($file, "r");
	    	while ( $l = fgets($fp, 1024)) {
		        if (!xml_parse($parser, $l, feof($fp))) echo "GLYPHE Error : Fichier de parametre incorrect ou mal structure.";
		    }
		    xml_parser_free($parser);
		    fclose($fp);
        } else {
        	echo "GLYPHE Error : Fichier de paramatre inaccessible.";
        }
	    
	}
	
	/**
     * Enter description here ...
     * @param unknown_type $parser
     * @param unknown_type $tag
     * @param unknown_type $attributes
     */
    function tag_open($parser, $tag, $attributes) {
        $this->tag = $tag;
    }

    /**
     * Enter description here ...
     * @param unknown_type $parser
     * @param unknown_type $cdata
     */
    function cdata($parser, $cdata) {
        if (isset($this->tag) && strtoupper($this->tag) != 'PARAM' && strtoupper($this->tag) != 'GLYPHE') $this->setParam(strtoupper($this->tag), $cdata);
    }

    /**
     * Enter description here ...
     * @param unknown_type $parser
     * @param unknown_type $tag
     */
    function tag_close($parser, $tag) {
        if ($this->tag === $tag) $this->tag = null;
    }
    
    /**
	 * Enter description here ...
	 * @param unknown_type $prop
	 * @param unknown_type $val
	 */
	private function setParam($prop, $val) {
		$this->$prop = $val;
	}
}
?>